package it.ap.sa.acqua.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import it.ap.sa.acqua.dao.model.TTankAlert;
import it.ap.sa.core.dao.util.AbstractGenericEntityDao;
import it.ap.sa.core.utils.LogUtil;
/**
 * @author Alessandro Paolillo
 */
@Repository
public class TTankAlertDao  extends AbstractGenericEntityDao<TTankAlert>{

	/**
	 * logger
	 */
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	/**
	 * @author Alessandro Paolillo
	 */
	@Transactional
	public TTankAlert findByPk(long id) throws Exception{
		try{
			String  hql     = "from " + TTankAlert.class.getName() + " where id = :id";
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(hql);

			query.setLong("id", id);

			return (TTankAlert)query.uniqueResult();
		}catch(Exception e){
			if(logger.isDebugEnabled())
				logger.debug("Error in findByPk", e);
			throw e;
		}
	}
	/**
	 * @author Alessandro Paolillo
	 */
	public List<TTankAlert> findByPks(List<Long> ids) throws Exception{
		try{
			String  hql     = "from " + TTankAlert.class.getName() + " where id in (:ids)";
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(hql);

			query.setParameterList("ids", ids);

			return query.list();
		}catch(Exception e){
			if(logger.isDebugEnabled())
				logger.debug("Error in findByPks", e);
			throw e;
		}
	}
	
	/**
	 * @author Alessandro Paolillo
	 */
	public List<TTankAlert> getTankAlerts() throws Exception{
		try{
			String  hql     = "from " + TTankAlert.class.getName();
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(hql);

			return query.list();
		}catch(Exception e){
			if(logger.isDebugEnabled())
				logger.debug("Error in getTankAlerts", e);
			throw e;
		}
	}
	
	/**
	 * Questo metodo elimina una vasca dato l'id della vasca e l'id dell'utente che la possiede
	 * @param id
	 * @param idUser
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public void deleteById(Long id, Long idUser) throws Exception {

		String    idLog     = "deleteById";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info(idLog);
		try {
			
			String sql = "DELETE FROM "+TTankAlert.class.getName()+" WHERE id = :id AND idUser = :idUser";
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(sql);
			query.setLong("id"			,id);
			query.setLong("idUser"		,idUser);
			query.executeUpdate();
		} catch (Exception ex) {

			logger.error("Si è verificato un errore in deleteById: " + ex.getMessage() , ex);
			throw(ex);
		} finally{

			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	/**
	 * Questo metodo elimina una vasca dato l'id della vasca e l'id dell'utente che la possiede
	 * @param id
	 * @param idUser
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public void deleteByIdTank(Long idTank) throws Exception {

		String    idLog     = "deleteByIdTank";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info(idLog);
		try {
			
			String sql = "DELETE FROM "+TTankAlert.class.getName()+" WHERE idTank = :idTank";
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(sql);
			query.setLong("idTank"		,idTank);
			query.executeUpdate();
		} catch (Exception ex) {

			logger.error("Si è verificato un errore in deleteByIdTank: " + ex.getMessage() , ex);
			throw(ex);
		} finally{

			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	/**
	 * @author Alessandro Paolillo
	 * @param {@link TTankAlert} tTankAlert
	 * @throws Exception
	 */
	@Transactional
	public TTankAlert saveOrUpdate(TTankAlert tTankAlert) throws Exception {

		String    idLog     = "saveOrUpdate";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info(idLog);
		try {
			
			saveOrUpdateEntity(tTankAlert);
		} catch (Exception ex) {

			logger.error("Si è verificato un errore in saveOrUpdate: " + ex.getMessage() , ex);
			throw(ex);
		} finally{

			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return tTankAlert;
	}
	
	/**
	 * @author Alessandro Paolillo
	 */
	public List<TTankAlert> getTankAlertsByIdTank( Long idTank ) throws Exception{
		try{
			String  hql     = "from " + TTankAlert.class.getName()+" WHERE idTank = :idTank";
			Session session = this.getSessionFactory().getCurrentSession();
			Query   query   = session.createQuery(hql);
			
			query.setLong("idTank"		,idTank);
			
			return query.list();
		}catch(Exception e){
			if(logger.isDebugEnabled())
				logger.debug("Error in getTankTypeAlerts", e);
			throw e;
		}
	}
}